package com.origin.netlibrary.download

import com.jeremyliao.liveeventbus.LiveEventBus
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadListener
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.model.FileDownloadStatus
import com.origin.netlibrary.NetUtils
import com.origin.netlibrary.bean.DownLoadProBean
import com.origin.utils.log.LogUtils


object DownloadManager {

    fun pause(url: String, path: String) {
        if (!downLoadMap.keys.contains(url)){
            return
        }
        var id= downLoadMap[url]

        FileDownloader.setup(NetUtils.getApplication())
        id?.apply {
            FileDownloader.getImpl().pause(this)
            LogUtils.__e("isDownLoading  pause   暂停  ${this}")
            val downLoadProBean = DownLoadProBean(url, -1, false, path, 0, true)
            LiveEventBus.get<DownLoadProBean>(DownLoadProBean::class.java).post(downLoadProBean)
        }

    }

    fun isDownLoading(url: String, path: String): Boolean {
        FileDownloader.setup(NetUtils.getApplication())
        val status = FileDownloader.getImpl().getStatus(url, path)
        LogUtils.__e("isDownLoading  status  $status")
        return status == FileDownloadStatus.progress
    }
    private var downLoadMap=HashMap<String,Int>()
    fun startDownLoad(url: String, path: String) {
        FileDownloader.setup(NetUtils.getApplication())
      val id=  FileDownloader.getImpl().create(url)
            .setForceReDownload(true)
            .setPath(path)
            .setListener(object : FileDownloadListener() {
                override fun pending(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    // 等待，已经进入下载队列
                    LogUtils.__e("pending  ${task} , soFarBytes  ${soFarBytes}  , totalBytes  ${totalBytes}")
                }

                override fun connected(
                    task: BaseDownloadTask,
                    etag: String,
                    isContinue: Boolean,
                    soFarBytes: Int,
                    totalBytes: Int
                ) {
                    LogUtils.__e("connected  ${task} ,  etag   ${etag}  isContinue  ${isContinue} , soFarBytes  ${soFarBytes}  , totalBytes  ${totalBytes}")
                }

                override fun progress(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {

                    val percent: Float = soFarBytes.toFloat() / totalBytes * 100
                    // DownLoadProBean
                    val downLoadProBean = DownLoadProBean(
                        task.url,
                        percent.toInt(),
                        false,
                        task.path,
                        task.speed,
                        false
                    )
//                    LogUtils.__e("progress  $downLoadProBean")
                    LiveEventBus.get<DownLoadProBean>(DownLoadProBean::class.java)
                        .post(downLoadProBean)
                }

                override fun blockComplete(task: BaseDownloadTask) {
                    // 在完成前同步调用该方法，此时已经下载完成
                    LogUtils.__e("blockComplete  ${task} ")
                }

                override fun retry(
                    task: BaseDownloadTask,
                    ex: Throwable,
                    retryingTimes: Int,
                    soFarBytes: Int
                ) {
                    LogUtils.__e("retry  task  ${task}  , ex ${ex} , retryingTimes${retryingTimes} , soFarBytes  ${soFarBytes}")
                }

                override fun completed(task: BaseDownloadTask) {
                    LogUtils.__e("completed  task  ${task}  ")
                    val downLoadProBean =
                        DownLoadProBean(task.url, 100, true, task.path, task.speed, false)
                    LiveEventBus.get<DownLoadProBean>(DownLoadProBean::class.java)
                        .post(downLoadProBean)
                    downLoadMap.remove(url)
                }

                override fun paused(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    LogUtils.__e("paused  task  ${task} ,  soFarBytes  ${soFarBytes}  , totalBytes  ${totalBytes}")
                }

                override fun error(task: BaseDownloadTask, e: Throwable) {
                    LogUtils.__e("error  task  ${task} ,  e  ${e}  ")
                }

                override fun warn(task: BaseDownloadTask) {
                    LogUtils.__e("warn  task  ${task}  ")
                }
            }).start()
        downLoadMap[url] = id
    }
}
